<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class REXML::Elements - rexml: Ruby Standard Library Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link">Object
</div>

        
<div id="includes-section" class="nav-section">
  <h3>Included Modules</h3>

  <ul class="link-list">
    <li><span class="include">Enumerable</span>
  </ul>
</div>

        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-i-3C-3C">#&lt;&lt;</a>
    <li ><a href="#method-i-5B-5D">#[]</a>
    <li ><a href="#method-i-5B-5D-3D">#[]=</a>
    <li ><a href="#method-i-add">#add</a>
    <li ><a href="#method-i-collect">#collect</a>
    <li ><a href="#method-i-delete">#delete</a>
    <li ><a href="#method-i-delete_all">#delete_all</a>
    <li ><a href="#method-i-each">#each</a>
    <li ><a href="#method-i-empty-3F">#empty?</a>
    <li ><a href="#method-i-index">#index</a>
    <li ><a href="#method-i-inject">#inject</a>
    <li ><a href="#method-i-literalize">#literalize</a>
    <li ><a href="#method-i-parent">#parent</a>
    <li ><a href="#method-i-size">#size</a>
    <li ><a href="#method-i-to_a">#to_a</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-REXML::Elements">
    <h1 id="class-REXML::Elements" class="class">
      class REXML::Elements
    </h1>

    <section class="description">
    
<p>A class which provides filtering of children for <a href="Elements.html"><code>Elements</code></a>, and <a href="XPath.html"><code>XPath</code></a> search support.  You are expected to only encounter this class as the <code>element.elements</code> object.  Therefore, you are <em>not</em> expected to instantiate this yourself.</p>

<pre class="ruby"><span class="ruby-identifier">xml_string</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;bookstore&gt;
  &lt;book category=&quot;cooking&quot;&gt;
    &lt;title lang=&quot;en&quot;&gt;Everyday Italian&lt;/title&gt;
    &lt;author&gt;Giada De Laurentiis&lt;/author&gt;
    &lt;year&gt;2005&lt;/year&gt;
    &lt;price&gt;30.00&lt;/price&gt;
  &lt;/book&gt;
  &lt;book category=&quot;children&quot;&gt;
    &lt;title lang=&quot;en&quot;&gt;Harry Potter&lt;/title&gt;
    &lt;author&gt;J K. Rowling&lt;/author&gt;
    &lt;year&gt;2005&lt;/year&gt;
    &lt;price&gt;29.99&lt;/price&gt;
  &lt;/book&gt;
  &lt;book category=&quot;web&quot;&gt;
    &lt;title lang=&quot;en&quot;&gt;XQuery Kick Start&lt;/title&gt;
    &lt;author&gt;James McGovern&lt;/author&gt;
    &lt;author&gt;Per Bothner&lt;/author&gt;
    &lt;author&gt;Kurt Cagle&lt;/author&gt;
    &lt;author&gt;James Linn&lt;/author&gt;
    &lt;author&gt;Vaidyanathan Nagarajan&lt;/author&gt;
    &lt;year&gt;2003&lt;/year&gt;
    &lt;price&gt;49.99&lt;/price&gt;
  &lt;/book&gt;
  &lt;book category=&quot;web&quot; cover=&quot;paperback&quot;&gt;
    &lt;title lang=&quot;en&quot;&gt;Learning XML&lt;/title&gt;
    &lt;author&gt;Erik T. Ray&lt;/author&gt;
    &lt;year&gt;2003&lt;/year&gt;
    &lt;price&gt;39.95&lt;/price&gt;
  &lt;/book&gt;
&lt;/bookstore&gt;
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-new" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(parent) &rarr; new_elements_object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns a new Elements object with the given <code>parent</code>. Does <em>not</em> assign <code>parent.elements = self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">eles</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Elements</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>)
<span class="ruby-identifier">eles</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
<span class="ruby-identifier">eles</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1608</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span> <span class="ruby-identifier">parent</span>
  <span class="ruby-ivar">@element</span> = <span class="ruby-identifier">parent</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-3C-3C" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">&lt;&lt;</span><span
                                class="method-args">(element=nil)</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Elements.html#method-i-add">add</a>
                            </div>
                          </div>

                  <div id="method-i-5B-5D" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          elements[index] &rarr; element or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          elements[xpath] &rarr; element or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          elements[n, name] &rarr; element or nil
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the first Element object selected by the arguments, if any found, or <code>nil</code> if none found.</p>

<p>Notes:</p>
<ul><li>
<p>The <code>index</code> is 1-based, not 0-based, so that:</p>
<ul><li>
<p>The first element has index <code>1</code></p>
</li><li>
<p>The <em>nth</em> element has index <code>n</code>.</p>
</li></ul>
</li><li>
<p>The selection ignores non-Element nodes.</p>
</li></ul>

<p>When the single argument <code>index</code> is given, returns the element given by the index, if any; otherwise, <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">eles</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">eles</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">1</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;cooking&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">4</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;web&#39; cover=&#39;paperback&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">5</span>] <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>The node at this index is not an Element, and so is not returned:</p>

<pre class="ruby"><span class="ruby-identifier">eles</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">first</span> <span class="ruby-comment"># =&gt; &lt;title lang=&#39;en&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [&quot;Everyday Italian&quot;]</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">1</span>] <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>When the single argument <code>xpath</code> is given, returns the first element found via that <code>xpath</code>, if any; otherwise, <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-identifier">eles</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;/bookstore&#39;</span>]                    <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;//book&#39;</span>]                        <span class="ruby-comment"># =&gt; &lt;book category=&#39;cooking&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;//book [@category=&quot;children&quot;]&#39;</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;/nosuch&#39;</span>]                       <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;//nosuch&#39;</span>]                      <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;//book [@category=&quot;nosuch&quot;]&#39;</span>]   <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;.&#39;</span>]                             <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-string">&#39;..&#39;</span>].<span class="ruby-identifier">class</span>                      <span class="ruby-comment"># =&gt; REXML::Document</span>
</pre>

<p>With arguments <code>n</code> and <code>name</code> given, returns the <em>nth</em> found element that has the given <code>name</code>, or <code>nil</code> if there is no such <em>nth</em> element:</p>

<pre class="ruby"><span class="ruby-identifier">eles</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">1</span>, <span class="ruby-string">&#39;book&#39;</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;cooking&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">4</span>, <span class="ruby-string">&#39;book&#39;</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;web&#39; cover=&#39;paperback&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">5</span>, <span class="ruby-string">&#39;book&#39;</span>] <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="5B-5D-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1680</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">[]</span>( <span class="ruby-identifier">index</span>, <span class="ruby-identifier">name</span>=<span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">index</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Integer</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;index (#{index}) must be &gt;= 1&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">index</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span>
    <span class="ruby-identifier">name</span> = <span class="ruby-identifier">literalize</span>(<span class="ruby-identifier">name</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">name</span>
    <span class="ruby-identifier">num</span> = <span class="ruby-value">0</span>
    <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">find</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">child</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">child</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span> <span class="ruby-keyword">and</span>
      (<span class="ruby-identifier">name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">?</span> <span class="ruby-keyword">true</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">child</span>.<span class="ruby-identifier">has_name?</span>( <span class="ruby-identifier">name</span> )) <span class="ruby-keyword">and</span>
      (<span class="ruby-identifier">num</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>) <span class="ruby-operator">==</span> <span class="ruby-identifier">index</span>
    }
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">return</span> <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier">first</span>( <span class="ruby-ivar">@element</span>, <span class="ruby-identifier">index</span> )
    <span class="ruby-comment">#{ |element|</span>
    <span class="ruby-comment">#       return element if element.kind_of? Element</span>
    <span class="ruby-comment">#}</span>
    <span class="ruby-comment">#return nil</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-5B-5D-3D" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          elements[] = index, replacement_element &rarr; replacement_element or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Replaces or adds an element.</p>

<p>When <code>eles[index]</code> exists, replaces it with <code>replacement_element</code> and returns <code>replacement_element</code>:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">eles</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">1</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;cooking&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">1</span>] = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>)
<span class="ruby-identifier">eles</span>[<span class="ruby-value">1</span>] <span class="ruby-comment"># =&gt; &lt;foo/&gt;</span>
</pre>

<p>Does nothing (or raises an exception) if <code>replacement_element</code> is not an Element:</p>

<pre class="ruby"><span class="ruby-identifier">eles</span>[<span class="ruby-value">2</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;web&#39; cover=&#39;paperback&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">2</span>] = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Text</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;bar&#39;</span>)
<span class="ruby-identifier">eles</span>[<span class="ruby-value">2</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;web&#39; cover=&#39;paperback&#39;&gt; ... &lt;/&gt;</span>
</pre>

<p>When <code>eles[index]</code> does not exist, adds <code>replacement_element</code> to the element and returns</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">eles</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span> <span class="ruby-comment"># =&gt; #&lt;REXML::Elements @element=&lt;bookstore&gt; ... &lt;/&gt;&gt;</span>
<span class="ruby-identifier">eles</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">50</span>] = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; &lt;foo/&gt;</span>
<span class="ruby-identifier">eles</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 5</span>
<span class="ruby-identifier">eles</span>[<span class="ruby-value">5</span>] <span class="ruby-comment"># =&gt; &lt;foo/&gt;</span>
</pre>

<p>Does nothing (or raises an exception) if <code>replacement_element</code> is not an Element:</p>

<pre class="ruby"><span class="ruby-identifier">eles</span>[<span class="ruby-value">50</span>] = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Text</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;bar&#39;</span>) <span class="ruby-comment"># =&gt; &quot;bar&quot;</span>
<span class="ruby-identifier">eles</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 5</span>
</pre>

                              <div class="method-source-code" id="5B-5D-3D-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1735</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">[]=</span>( <span class="ruby-identifier">index</span>, <span class="ruby-identifier">element</span> )
  <span class="ruby-identifier">previous</span> = <span class="ruby-keyword">self</span>[<span class="ruby-identifier">index</span>]
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">previous</span>.<span class="ruby-identifier">nil?</span>
    <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">previous</span>.<span class="ruby-identifier">replace_with</span> <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">previous</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-add" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          add &rarr; new_element
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          add(name) &rarr; new_element
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          add(element) &rarr; element
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Adds an element; returns the element added.</p>

<p>With no argument, creates and adds a new element. The new element has:</p>
<ul><li>
<p>No name.</p>
</li><li>
<p>Parent from the Elements object.</p>
</li><li>
<p>Context from the that parent.</p>
</li></ul>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">parent</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">parent</span>     <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">parent</span>.<span class="ruby-identifier">context</span> = {<span class="ruby-value">raw:</span> <span class="ruby-value">:all</span>}
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span>                <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">new_element</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">add</span>   <span class="ruby-comment"># =&gt; &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span>                <span class="ruby-comment"># =&gt; 5</span>
<span class="ruby-identifier">new_element</span>.<span class="ruby-identifier">name</span>             <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">new_element</span>.<span class="ruby-identifier">parent</span>           <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">new_element</span>.<span class="ruby-identifier">context</span>          <span class="ruby-comment"># =&gt; {:raw=&gt;:all}</span>
</pre>

<p>With string argument <code>name</code>, creates and adds a new element. The new element has:</p>
<ul><li>
<p>Name <code>name</code>.</p>
</li><li>
<p>Parent from the Elements object.</p>
</li><li>
<p>Context from the that parent.</p>
</li></ul>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">parent</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">parent</span>          <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">parent</span>.<span class="ruby-identifier">context</span> = {<span class="ruby-value">raw:</span> <span class="ruby-value">:all</span>}
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span>                     <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">new_element</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">add</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; &lt;foo/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span>                     <span class="ruby-comment"># =&gt; 5</span>
<span class="ruby-identifier">new_element</span>.<span class="ruby-identifier">name</span>                  <span class="ruby-comment"># =&gt; &quot;foo&quot;</span>
<span class="ruby-identifier">new_element</span>.<span class="ruby-identifier">parent</span>                <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">new_element</span>.<span class="ruby-identifier">context</span>               <span class="ruby-comment"># =&gt; {:raw=&gt;:all}</span>
</pre>

<p>With argument <code>element</code>, creates and adds a clone of the given <code>element</code>. The new element has name, parent, and context from the given <code>element</code>.</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span>                 <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">e0</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>)
<span class="ruby-identifier">e1</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-identifier">e0</span>, {<span class="ruby-value">raw:</span> <span class="ruby-value">:all</span>})
<span class="ruby-identifier">element</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">e1</span>) <span class="ruby-comment"># =&gt; &lt;bar/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span>                 <span class="ruby-comment"># =&gt; 5</span>
<span class="ruby-identifier">element</span>.<span class="ruby-identifier">name</span>                  <span class="ruby-comment"># =&gt; &quot;bar&quot;</span>
<span class="ruby-identifier">element</span>.<span class="ruby-identifier">parent</span>                <span class="ruby-comment"># =&gt; &lt;bookstore&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">element</span>.<span class="ruby-identifier">context</span>               <span class="ruby-comment"># =&gt; {:raw=&gt;:all}</span>
</pre>

                              <div class="method-source-code" id="add-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1925</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add</span> <span class="ruby-identifier">element</span>=<span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">nil?</span>
    <span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;&quot;</span>, <span class="ruby-keyword">self</span>, <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">context</span>)
  <span class="ruby-keyword">elsif</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Element</span>)
    <span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">element</span>, <span class="ruby-keyword">self</span>, <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">context</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-ivar">@element</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">element</span>
    <span class="ruby-identifier">element</span>.<span class="ruby-identifier">context</span> = <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">context</span>
    <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Elements.html#method-i-3C-3C">&lt;&lt;</a>
                            </div>

                          </div>

                  <div id="method-i-collect" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          collect(xpath = nil) {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Iterates over the elements; returns the array of block return values.</p>

<p>With no argument, iterates over all elements:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">collect</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; [9, 9, 17, 9]</span>
</pre>

<p>With argument <code>xpath</code>, iterates over elements that match the given <code>xpath</code>:</p>

<pre class="ruby"><span class="ruby-identifier">xpath</span> = <span class="ruby-string">&#39;//book [@category=&quot;web&quot;]&#39;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">collect</span>(<span class="ruby-identifier">xpath</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; [17, 9]</span>
</pre>

                              <div class="method-source-code" id="collect-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1988</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">collect</span>( <span class="ruby-identifier">xpath</span>=<span class="ruby-keyword">nil</span> )
  <span class="ruby-identifier">collection</span> = []
  <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier">each</span>( <span class="ruby-ivar">@element</span>, <span class="ruby-identifier">xpath</span> ) {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">collection</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword">yield</span>(<span class="ruby-identifier">e</span>)  <span class="ruby-keyword">if</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Element</span>)
  }
  <span class="ruby-identifier">collection</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-delete" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          delete(index) &rarr; removed_element or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          delete(element) &rarr; removed_element or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          delete(xpath) &rarr; removed_element or nil
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Removes an element; returns the removed element, or <code>nil</code> if none removed.</p>

<p>With integer argument <code>index</code> given, removes the child element at that offset:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">elements</span>[<span class="ruby-value">2</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; &lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">elements</span>[<span class="ruby-value">2</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;web&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">50</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With element argument <code>element</code> given, removes that child element:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">ele_1</span>, <span class="ruby-identifier">ele_2</span>, <span class="ruby-identifier">ele_3</span>, <span class="ruby-identifier">ele_4</span> = <span class="ruby-operator">*</span><span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">elements</span>[<span class="ruby-value">2</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">ele_2</span>) <span class="ruby-comment"># =&gt; &lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">elements</span>[<span class="ruby-value">2</span>] <span class="ruby-comment"># =&gt; &lt;book category=&#39;web&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">ele_2</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With string argument <code>xpath</code> given, removes the first element found via that xpath:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&#39;//book&#39;</span>) <span class="ruby-comment"># =&gt; &lt;book category=&#39;cooking&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&#39;//book [@category=&quot;children&quot;]&#39;</span>) <span class="ruby-comment"># =&gt; &lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&#39;//nosuch&#39;</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="delete-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1825</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">delete</span> <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span>
    <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">el</span> = <span class="ruby-keyword">self</span>[<span class="ruby-identifier">element</span>]
    <span class="ruby-identifier">el</span>.<span class="ruby-identifier">remove</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">el</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-delete_all" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          delete_all(xpath)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Removes all elements found via the given <code>xpath</code>; returns the array of removed elements, if any, else <code>nil</code>.</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">deleted_elements</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete_all</span>(<span class="ruby-string">&#39;//book [@category=&quot;web&quot;]&#39;</span>)
<span class="ruby-identifier">deleted_elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">deleted_elements</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete_all</span>(<span class="ruby-string">&#39;//book&#39;</span>)
<span class="ruby-identifier">deleted_elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete_all</span>(<span class="ruby-string">&#39;//book&#39;</span>) <span class="ruby-comment"># =&gt; []</span>
</pre>

                              <div class="method-source-code" id="delete_all-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1851</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">delete_all</span>( <span class="ruby-identifier">xpath</span> )
  <span class="ruby-identifier">rv</span> = []
  <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier">each</span>( <span class="ruby-ivar">@element</span>, <span class="ruby-identifier">xpath</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">rv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">element</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span>
  }
  <span class="ruby-identifier">rv</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
    <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">element</span>
    <span class="ruby-identifier">element</span>.<span class="ruby-identifier">remove</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">rv</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each(xpath = nil) {|element| ... } &rarr; self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Iterates over the elements.</p>

<p>With no argument, calls the block with each element:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">element</span> }
</pre>

<p>Output:</p>

<pre>&lt;book category=&#39;cooking&#39;&gt; ... &lt;/&gt;
&lt;book category=&#39;children&#39;&gt; ... &lt;/&gt;
&lt;book category=&#39;web&#39;&gt; ... &lt;/&gt;
&lt;book category=&#39;web&#39; cover=&#39;paperback&#39;&gt; ... &lt;/&gt;</pre>

<p>With argument <code>xpath</code>, calls the block with each element that matches the given <code>xpath</code>:</p>

<pre class="ruby"><span class="ruby-identifier">elements</span>.<span class="ruby-identifier">each</span>(<span class="ruby-string">&#39;//book [@category=&quot;web&quot;]&#39;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">element</span> }
</pre>

<p>Output:</p>

<pre>&lt;book category=&#39;web&#39;&gt; ... &lt;/&gt;
&lt;book category=&#39;web&#39; cover=&#39;paperback&#39;&gt; ... &lt;/&gt;</pre>

                              <div class="method-source-code" id="each-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1967</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">each</span>( <span class="ruby-identifier">xpath</span>=<span class="ruby-keyword">nil</span> )
  <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier">each</span>( <span class="ruby-ivar">@element</span>, <span class="ruby-identifier">xpath</span> ) {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-keyword">yield</span> <span class="ruby-identifier">e</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span> }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-empty-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          empty? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns <code>true</code> if there are no children, <code>false</code> otherwise.</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>)
<span class="ruby-identifier">d</span>.<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">d</span>.<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="empty-3F-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1755</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">empty?</span>
  <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">find</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">child</span><span class="ruby-operator">|</span> <span class="ruby-identifier">child</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span>}.<span class="ruby-identifier">nil?</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-index" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          index(element)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the 1-based index of the given <code>element</code>, if found; otherwise, returns -1:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">ele_1</span>, <span class="ruby-identifier">ele_2</span>, <span class="ruby-identifier">ele_3</span>, <span class="ruby-identifier">ele_4</span> = <span class="ruby-operator">*</span><span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">ele_4</span>) <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">ele_3</span>)
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">ele_4</span>) <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">ele_3</span>) <span class="ruby-comment"># =&gt; -1</span>
</pre>

                              <div class="method-source-code" id="index-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1773</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">index</span> <span class="ruby-identifier">element</span>
  <span class="ruby-identifier">rv</span> = <span class="ruby-value">0</span>
  <span class="ruby-identifier">found</span> = <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">find</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">child</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">child</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span> <span class="ruby-keyword">and</span>
    (<span class="ruby-identifier">rv</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>) <span class="ruby-keyword">and</span>
    <span class="ruby-identifier">child</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">rv</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">found</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">element</span>
  <span class="ruby-keyword">return</span> <span class="ruby-value">-1</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-inject" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          inject(xpath = nil, initial = nil) &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with elements; returns the last block return value.</p>

<p>With no argument, iterates over the elements, calling the block <code>elements.size - 1</code> times.</p>
<ul><li>
<p>The first call passes the first and second elements.</p>
</li><li>
<p>The second call passes the first block return value and the third element.</p>
</li><li>
<p>The third call passes the second block return value and the fourth element.</p>
</li><li>
<p>And so on.</p>
</li></ul>

<p>In this example, the block returns the passed element, which is then the object argument to the next call:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">inject</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">object</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> [<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">object</span>), <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">element</span>)]
  <span class="ruby-identifier">element</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">1</span>, <span class="ruby-value">2</span>]
[<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]
[<span class="ruby-value">3</span>, <span class="ruby-value">4</span>]
</pre>

<p>With the single argument <code>xpath</code>, calls the block only with elements matching that xpath:</p>

<pre class="ruby"><span class="ruby-identifier">elements</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;//book [@category=&quot;web&quot;]&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">object</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> [<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">object</span>), <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">element</span>)]
  <span class="ruby-identifier">element</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">3</span>, <span class="ruby-value">4</span>]
</pre>

<p>With argument <code>xpath</code> given as <code>nil</code> and argument <code>initial</code> also given, calls the block once for each element.</p>
<ul><li>
<p>The first call passes the <code>initial</code> and the first element.</p>
</li><li>
<p>The second call passes the first block return value and the second element.</p>
</li><li>
<p>The third call passes the second block return value and the third element.</p>
</li><li>
<p>And so on.</p>
</li></ul>

<p>In this example, the first object index is <code>-1</code></p>

<pre class="ruby"><span class="ruby-identifier">elements</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-keyword">nil</span>, <span class="ruby-string">&#39;Initial&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">object</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> [<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">object</span>), <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">element</span>)]
  <span class="ruby-identifier">element</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">-1</span>, <span class="ruby-value">1</span>]
[<span class="ruby-value">1</span>, <span class="ruby-value">2</span>]
[<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]
[<span class="ruby-value">3</span>, <span class="ruby-value">4</span>]
</pre>

<p>In this form the passed object can be used as an accumulator:</p>

<pre class="ruby"><span class="ruby-identifier">elements</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-keyword">nil</span>, <span class="ruby-value">0</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">total</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">total</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">size</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; 44</span>
</pre>

<p>With both arguments <code>xpath</code> and <code>initial</code> are given, calls the block only with elements matching that xpath:</p>

<pre class="ruby"><span class="ruby-identifier">elements</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;//book [@category=&quot;web&quot;]&#39;</span>, <span class="ruby-value">0</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">total</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">total</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">size</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; 26</span>
</pre>

                              <div class="method-source-code" id="inject-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 2073</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">inject</span>( <span class="ruby-identifier">xpath</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">initial</span>=<span class="ruby-keyword">nil</span> )
  <span class="ruby-identifier">first</span> = <span class="ruby-keyword">true</span>
  <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier">each</span>( <span class="ruby-ivar">@element</span>, <span class="ruby-identifier">xpath</span> ) {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> (<span class="ruby-identifier">e</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span>)
      <span class="ruby-keyword">if</span> (<span class="ruby-identifier">first</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">initial</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">nil</span>)
        <span class="ruby-identifier">initial</span> = <span class="ruby-identifier">e</span>
        <span class="ruby-identifier">first</span> = <span class="ruby-keyword">false</span>
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">initial</span> = <span class="ruby-keyword">yield</span>( <span class="ruby-identifier">initial</span>, <span class="ruby-identifier">e</span> ) <span class="ruby-keyword">if</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  }
  <span class="ruby-identifier">initial</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parent" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          parent
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the parent element cited in creating the Elements object. This element is also the default starting point for searching in the Elements object.</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">xml_string</span>)
<span class="ruby-identifier">elements</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Elements</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>)
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">parent</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span> <span class="ruby-comment"># =&gt; true</span>
</pre>

                              <div class="method-source-code" id="parent-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 1623</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parent</span>
  <span class="ruby-ivar">@element</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-size" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          size &rarr; integer
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the count of Element children:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;&lt;a&gt;sean&lt;b/&gt;elliott&lt;b/&gt;russell&lt;b/&gt;&lt;/a&gt;&#39;</span>
<span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3 # Three elements.</span>
<span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">size</span>          <span class="ruby-comment"># =&gt; 6 # Three elements plus three text nodes..</span>
</pre>

                              <div class="method-source-code" id="size-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 2097</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">size</span>
  <span class="ruby-identifier">count</span> = <span class="ruby-value">0</span>
  <span class="ruby-ivar">@element</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">child</span><span class="ruby-operator">|</span> <span class="ruby-identifier">count</span><span class="ruby-operator">+=</span><span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">child</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span> }
  <span class="ruby-identifier">count</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_a" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_a(xpath = nil) &rarr; array_of_elements
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of element children (not including non-element children).</p>

<p>With no argument, returns an array of all element children:</p>

<pre class="ruby"><span class="ruby-identifier">d</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;&lt;a&gt;sean&lt;b/&gt;elliott&lt;c/&gt;&lt;/a&gt;&#39;</span>
<span class="ruby-identifier">elements</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">elements</span>
<span class="ruby-identifier">elements</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [&lt;b/&gt;, &lt;c/&gt;]               # Omits non-element children.</span>
<span class="ruby-identifier">children</span> = <span class="ruby-identifier">d</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">children</span>
<span class="ruby-identifier">children</span> <span class="ruby-comment"># =&gt; [&quot;sean&quot;, &lt;b/&gt;, &quot;elliott&quot;, &lt;c/&gt;] # Includes non-element children.</span>
</pre>

<p>With argument <code>xpath</code>, returns an array of element children that match the xpath:</p>

<pre class="ruby"><span class="ruby-identifier">elements</span>.<span class="ruby-identifier">to_a</span>(<span class="ruby-string">&#39;//c&#39;</span>) <span class="ruby-comment"># =&gt; [&lt;c/&gt;]</span>
</pre>

                              <div class="method-source-code" id="to_a-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 2121</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_a</span>( <span class="ruby-identifier">xpath</span>=<span class="ruby-keyword">nil</span> )
  <span class="ruby-identifier">rv</span> = <span class="ruby-constant">XPath</span>.<span class="ruby-identifier">match</span>( <span class="ruby-ivar">@element</span>, <span class="ruby-identifier">xpath</span> )
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">rv</span>.<span class="ruby-identifier">find_all</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Element</span>} <span class="ruby-keyword">if</span> <span class="ruby-identifier">xpath</span>
  <span class="ruby-identifier">rv</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Instance Methods</h3>
                </header>

                  <div id="method-i-literalize" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">literalize</span><span
                                class="method-args">(name)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Private helper class.  Removes quotes from quoted strings</p>

                              <div class="method-source-code" id="literalize-source">
            <pre><span class="ruby-comment"># File rexml-3.2.6/lib/rexml/element.rb, line 2129</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">literalize</span> <span class="ruby-identifier">name</span>
  <span class="ruby-identifier">name</span> = <span class="ruby-identifier">name</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-2</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">name</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">?&#39;</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">name</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">?&quot;</span>               <span class="ruby-comment">#&#39;</span>
  <span class="ruby-identifier">name</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

